<template>
  <div>
    <div class="list-table mb-15">
      <div class="area-header">
        <span class="area-header-title">{{ $t('出资人信息') }}</span>
      </div>
      <el-form ref="queryForm" :model="queryParams" :inline="true">
        <!--        <el-form-item :label="$t('出资人属性')" prop="investorAttr">-->
        <!--          <el-select v-model="queryParams.investorAttr" filterable :placeholder="$t('出资人属性')" clearable>-->
        <!--            <el-option-->
        <!--              v-for="dict in investorAttrOptions"-->
        <!--              :key="dict.dictValue"-->
        <!--              :label="$t(dict.dictLabel)"-->
        <!--              :value="dict.dictValue"-->
        <!--            />-->
        <!--          </el-select>-->
        <!--        </el-form-item>-->
        <el-form-item :label="$t('出资人名称')" prop="investorName">
          <el-input
            v-model="queryParams.investorName"
            :placeholder="$t('出资人名称')"
            clearable
            @keyup.enter.native="queryHandle"
          />
        </el-form-item>
        <el-form-item :label="$t('出资人类型')" prop="investorType">
          <el-select v-model="queryParams.investorType" filterable :placeholder="$t('出资人类型')" clearable>
            <el-option
              v-for="dict in investorTypeOptions"
              :key="dict.dictValue"
              :label="$t(dict.dictLabel)"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">
            {{ $t('搜索') }}
          </el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t('重置') }}</el-button>
        </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            v-hasPermi="['bf:capital:edit']"
            type="primary"
            icon="el-icon-plus"
            size="mini"
            @click="addDealInvestmentHandle"
          >{{ $t('新增我方投资主体') }}
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            v-hasPermi="['bf:capital:edit']"
            type="primary"
            icon="el-icon-plus"
            size="mini"
            @click="addHandle"
          >{{ $t('新增出资人') }}
          </el-button>
        </el-col>
      </el-row>

      <el-table v-loading="loading" tooltip-effect="darkTable" :data="bfCapitalList" border>
        <el-table-column :label="$t('序号')" type="index" fixed="left" align="center" show-overflow-tooltip width="50">
          <template slot-scope="scope">
            {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
          </template>
        </el-table-column>
        <!--        <el-table-column :label="$t('出资人属性')" fixed="left" align="center" prop="investorAttr" :formatter="investorAttrFormat" min-width="130" />-->
        <el-table-column :label="$t('出资人名称')" fixed="left" align="center" prop="actTitle" :show-overflow-tooltip="true" min-width="200">
          <template slot-scope="scope">
            <span v-if="scope.row.dataSource === 'investment'" class="link-type" @click="updateHandle(scope.row)">{{ scope.row.investmentInvestorName }}</span>
            <span v-else class="link-type" @click="updateHandle(scope.row)">{{ scope.row.investorName }}</span>
          </template>
        </el-table-column>
        <el-table-column :label="$t('出资人类型')" align="center" prop="investorType" :formatter="investorTypeFormat" min-width="150" />
        <el-table-column :label="$t('认缴时间')" align="center" prop="subscribeTime" width="130">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.subscribeTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column :label="$t('认缴金额') + $t('金额单位')" align="center" prop="subscribeAmount" min-width="130">
          <template slot-scope="scope">
            <span class="money-text">{{ moneyFormat(scope.row.subscribeAmount, 'w') }}</span>
          </template>
        </el-table-column>
        <el-table-column :label="$t('认缴比例(%)')" align="center" prop="shareRatio" min-width="130">
          <template slot-scope="scope">
            <span>{{ moneyFormat(scope.row.shareRatio) }}</span>
          </template>
        </el-table-column>
        <el-table-column :label="$t('是否为投资机构')" align="center" prop="whetherInvestOrg" :formatter="whetherInvestOrgFormat" min-width="130" />
        <el-table-column :label="$t('累计实缴金额') + $t('金额单位')" align="center" prop="totalPaidIn" min-width="140">
          <template slot-scope="scope">
            <span class="money-text">{{ moneyFormat(scope.row.totalPaidIn, 'w') }}</span>
          </template>
        </el-table-column>
        <el-table-column
          :label="$t('操作')"
          fixed="right"
          align="center"
          class-name="small-padding fixed-width"
          width="140"
        >
          <template slot-scope="scope">
            <el-button
              v-hasPermi="['bf:capital:edit']"
              :disabled="scope.row.dataSource === 'investment'"
              size="small"
              type="text"
              icon="el-icon-edit"
              @click="updateHandle(scope.row)"
            >{{ $t('修改') }}
            </el-button>
            <el-button
              v-hasPermi="['bf:capital:remove']"
              :disabled="scope.row.dataSource === 'investment'"
              size="small"
              type="text"
              icon="el-icon-delete"
              @click="deleteHandle(scope.row)"
            >{{ $t('删除') }}
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
    <!-- 添加或修改出资人信息对话框 -->
    <el-dialog
      :title="$t(title)"
      :visible.sync="formCapitalOpen"
      :close-on-click-modal="false"
      :lock-scroll="false"
      custom-class="dialogForm"
      append-to-body
    >
      <bfCapitalForm ref="bfCapitalForm" />
    </el-dialog>

    <!-- 添加或修改投资主体对话框 -->
    <el-dialog :title="$t('投资主体')" :visible.sync="formOpen" :close-on-click-modal="false" :lock-scroll="false" :destroy-on-close="true" append-to-body custom-class="dialogForm">
      <dealInvestmentForm ref="dealInvestmentForm" />
    </el-dialog>
  </div>
</template>

<script>
import { listBfCapital, delBfCapital } from '@/api/bf/capital/bfCapital'
import bfCapitalForm from './bfCapitalForm.vue'
import dealInvestmentForm from '@/views/deal/investment/dealInvestmentForm'
export default {
  components: {
    bfCapitalForm,
    dealInvestmentForm
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 出资人信息表格数据
      bfCapitalList: [],
      // 弹出层标题
      title: '出资人信息',
      // 是否显示弹出层
      formOpen: false,
      formCapitalOpen: false,
      // 出资人属性字典
      investorAttrOptions: [],
      investorAttrTypeOptions: [],
      // 出资人类型字典
      investorTypeOptions: [],
      // 是否为投资机构字典
      whetherInvestOrgOptions: [],
      // 查询参数
      queryParams: {
        dealId: this.$route.query.id,
        investorAttr: undefined,
        investorType: undefined,
        investorName: undefined,
        pageNum: 1,
        pageSize: 10
      },
      // 主体类型字典
      mainBodyTypeOptions: [],
      // 币种字典
      currencyOptions: [],
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    }
  },
  created() {
    this.getList()
    this.getDicts('bf_investor_attr').then(response => {
      this.investorAttrOptions = response.data
    })
    this.getDicts('deal_investment_type').then(response => {
      this.investorAttrTypeOptions = response.data
    })
    this.getDicts('investor_type').then(response => {
      this.investorTypeOptions = response.data
    })
    this.getDicts('sys_yes_no').then(response => {
      this.whetherInvestOrgOptions = response.data
    })
  },
  methods: {
    /** 查询出资详情列表 */
    getList() {
      this.loading = true
      listBfCapital(this.queryParams).then(response => {
        this.bfCapitalList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 出资人属性字典翻译
    investorAttrFormat(row) {
      if (row.dataSource === 'investment') {
        return this.selectDictLabel(this.investorAttrTypeOptions, row.investorAttr)
      } else {
        return this.selectDictLabel(this.investorAttrOptions, row.investorAttr)
      }
    },
    // 出资人类型字典翻译
    investorTypeFormat(row) {
      return this.selectDictLabel(this.investorTypeOptions, row.investorType)
    },
    // 是否为投资机构字典翻译
    whetherInvestOrgFormat(row) {
      return this.selectDictLabel(this.whetherInvestOrgOptions, row.whetherInvestOrg)
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        dealId: this.$route.query.id,
        investorAttr: undefined,
        investorType: undefined,
        investorName: undefined,
        investmentInvestorName: undefined,
        subscribeTime: undefined,
        subscribeAmount: undefined,
        whetherInvestOrg: '0',
        totalPaidIn: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        delFlag: undefined,
        remark: undefined
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    queryHandle() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.queryHandle()
    },
    /** 新增按钮操作 */
    addHandle() {
      this.formCapitalOpen = true
      this.$nextTick(() => {
        this.$refs.bfCapitalForm.init(null)
      })
    },
    /** 新增我方投资主体 */
    addDealInvestmentHandle() {
      this.formOpen = true
      this.$nextTick(() => {
        this.$refs.dealInvestmentForm.init(null)
      })
    },
    /** 修改按钮操作 */
    updateHandle(row) {
      this.formCapitalOpen = true
      this.$nextTick(() => {
        // 调用form页面的 init 方法初始化数据
        this.$refs.bfCapitalForm.init(row)
      })
    },
    /** 删除按钮操作 */
    deleteHandle(row) {
      this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
        confirmButtonText: this.$t('确定'),
        cancelButtonText: this.$t('取消'),
        type: 'warning'
      }).then(function() {
        return delBfCapital(row.id)
      }).then(() => {
        this.getList()
        this.msgSuccess(this.$t('删除成功'))
      }).catch(function() {
      })
    }
  }
}
</script>
